package zzjson.rabbitmq.ttl;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import zzjson.rabbitmq.constants.RabbitConstants;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.rabbitmq.ttl</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年04月25日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class TTLProducer {
	public static void main(String[] args) throws IOException, TimeoutException {
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost(RabbitConstants.host);
		factory.setVirtualHost("/");
		factory.setPassword(RabbitConstants.password);
		factory.setUsername(RabbitConstants.UserName);

		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();
		Map<String, Object> arguments = new HashMap<>();
		arguments.put("x-message-ttl", 2000);//设置消息过期时间
		arguments.put("x-expires", 6000);//队列过期时间
		channel.queueDeclare("TEST_TTL_QUEUE", false, false, false, arguments);

		AMQP.BasicProperties properties = new AMQP.BasicProperties().builder().
				deliveryMode(2).//持久化消息
				contentEncoding("utf-8").
				// expiration("10000").//TTL 毫秒
						build();
		//	此处两种方式都设置了，会以最小的数值为准
		//	发送消息
		String msg = "hello mother fuck";
		channel.basicPublish("", "TEST_TTL_QUEUE", properties, msg.getBytes());
		channel.close();
		connection.close();

	}
}